//
//  NSObject+DY_EncryptionTools.m
//  deciphering
//
//  Created by LXF on 16/5/16.
//  Copyright © 2016年 LXF. All rights reserved.
//

#import "NSString+DY_EncryptionTools.h"

#import "NSString+Hash.h"

static NSString *salt = @"5ED8854161283A2A3EE892A1B700DC65" ; //约定好的 SALT

@implementation NSString (DY_EncryptionTools)

-(NSString *)dy_encryption1{
    return [self hmacSHA512StringWithKey:salt].md5String;
}

-(NSString *)dy_encryption2_0ByDate:(NSString *)dateStamp{
    return [self hmacSHA512StringWithKey:[dateStamp hmacSHA512StringWithKey:salt]];
}

-(NSString *)dy_encryption2_1ByDate:(NSString *)dateStamp{
    return [[self dy_encryption1]dy_encryption2_0ByDate:dateStamp];
}

+(NSString *)dy_generateHmacAuthCodeWithDeviceID:(NSString *)deviceID date:(NSString *)deviceTimestamp userID:(NSString *)currentUserID accesstoken:(NSString *)accesstoken{
    if (currentUserID == nil || [@"0" isEqual:currentUserID] || accesstoken == nil) {
        currentUserID = @"0";
        accesstoken = @"xfn100";
    }
    NSString *msg = [NSString stringWithFormat:@"%@%@%@%@",deviceID,deviceTimestamp,currentUserID,salt];
    return [msg hmacSHA512StringWithKey:accesstoken];
}
@end
